package com.cai.baseapplication.mvp.main

import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.cai.baseapplication.R
import com.cai.baseapplication.mvp.main.home.HomeFragment
import com.cai.baselibrary.arouter.ARouterPath
import com.cai.baselibrary.mvp.IView
import com.cai.baselibrary.mvp.presenter.IPresenter
import com.cai.baselibrary.mvp.view.BaseActivity
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import kotlinx.android.synthetic.main.activity_main.*

@Route(path = ARouterPath.ACTIVITY_MAIN)
class MainActivity : BaseActivity() {
    //标题
    private val titles = arrayOf("首页", "知识", "导航", "项目")
    private val fragmentList : MutableList<Fragment> = mutableListOf()

    override val mPresenter: IPresenter<out IView>
        get() = IPresenter(this)

    override fun getContentView(): Int = R.layout.activity_main

    override fun init() {
        //初始化fragment
        fragmentList.add(HomeFragment())
        fragmentList.add(HomeFragment())
        fragmentList.add(HomeFragment())
        fragmentList.add(HomeFragment())

        //初始化viewPage
        mViewPage!!.adapter = object: FragmentStateAdapter(this){
            override fun getItemCount(): Int {
                return fragmentList.size
            }

            override fun createFragment(position: Int): Fragment {
                return  fragmentList[position]
            }
        }
        val tabLayoutMediator = TabLayoutMediator(
            mTabLayout,mViewPage
        ) { tab: TabLayout.Tab, position: Int ->
                tab.text = titles[position]
        }
        tabLayoutMediator.attach()
    }



}